// const { it } = require("node:test");

//右侧时间
setInterval(function () {
  let month = document.querySelector('.month')
  let year = document.querySelector('.year')
  let weak = document.querySelector('.weak')
  month.innerHTML = str1        //替换内容为显示的时间
  year.innerHTML = str2        //替换内容为显示的时间
  weak.innerHTML = str3        //替�
}, 1000);

//生成时间对象，什么也不传，获取当前系统的时间
let D = new Date();

let nian = D.getFullYear();   //获取年
let yue = (D.getMonth() + 1).toString().padStart(2, 0);       //获取月 0-11
let ri = (D.getDate()).toString().padStart(2, 0);       //获取日
let xingqi = numOfChinese(D.getDay());      //获取星期几 0-6，0是星期日

// let shi = changeNum(D.getHours());      //获取小时
// let fen = D.getMinutes();     //获取分钟
// let miao = D.getSeconds();    //获取秒

/* 
    对数字进行调整
*/

//把星期的数字换成汉字
function numOfChinese(num) {
  let arr = ['日', '一', '二', '三', '四', '五', '六'];
  return arr[num];
}
//小时前面加上0
function changeNum(num) {
  if (num < 10) {
    return "0" + num;
  } else {
    return num;
  }
}
let str1 = `${yue}/${ri}`
let str2 = `/ ${nian} `
let str3 = `星期${xingqi} `

localStorage.setItem("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzMjE5N2JkOGMwZmQzMzNmNjQ2ZDE3YyIsImlhdCI6MTY2MzQ2NzUxOCwiZXhwIjoxNjYzNjQwMzE4fQ.PFHCP6iDvZzvk0BUZljrBV8REEqeYiH8Ui6UcHKt-tk");




let diaryEl = document.querySelector('.diary')

getNotebooks()

async function getNotebooks() {
  let res1 = await axios({
    method: "GET",
    url: "/api/user",
    params: {
      userId: "632197bd8c0fd333f646d17c",
    },
  });
  let userName = res1.data.data.username
  //获取日记名
  let res4 = await axios({
    method: "GET",
    url: "/api/diarybook",
    params: {
      authorId: "632197bd8c0fd333f646d17c",
    },
  });
  let notebooksLists = res4.data.data.diarybooks;
  console.log(37, notebooksLists);
  for (let i = 0; i < notebooksLists.length; i++) {
    firstName = notebooksLists[i].title
    // console.log(39, firstName);
  }



  //获取单个日记
  let res5 = await axios({
    method: "GET",
    url: "/api/diaryItem",
    params: {
      diaryItemId: '632325927542a3c04fc72c72',
    },

  })
  // console.log(87, res5);
  let riJI = res5.data.data.diaryItem;
  // console.log(89, riJI);
  html = `
      <div class="yu">
          <span>${firstName}</span>
          <div class="line"></div>
          <div class="timeUp">${riJI.createAt}pm</div>
          <div class="day">
            <img class="img1" src="" alt="">
            <p class="text2">${riJI.content}</p>
          </div>
          <div class="line"></div>
        </div>
 `
  diaryEl.innerHTML = html

}
// ...........................changeNum....................

let user = localStorage.user && JSON.parse(localStorage.user)
console.log(789, user);

let commentsEl = document.querySelector('.comments');      
// console.log(commentsEl); //获取回复
//获取所有回复
getComments()
async function getComments(){
  //请求作者名字
  let res3 = await axios({
    method: 'GET',
    url: '/api/user',
    params: {
      userId: user.userId
    }
  })
  // console.log(666, res3);
  let userN = res3.data.data
  // console.log(9898, userN);
  //发请求
  let res2 = await axios({
    method: 'GET',
    url: '/api/reply',
    params: {
      diaryItemId: '6322a2fd9d798f6c5224622d'
    }
  })
// console.log(33, res2);
  let deplay = res2.data.data
   console.log(1000,deplay);
  html1 = ''
  for (const item of deplay) {
    html1 += `
     <div class="ry">
            <img src="http://localhost:8000${userN.headphoto}" alt="">
            <div class="body">
              <span class="name">${userN.username}</span>
              <span class="time">${item.createAt}</span>
              <div class="tit">${item.content}</div>
              <button class="tu">回复</button>
            </div>
          </div>
  
  `
}
  commentsEl.innerHTML = html1




  //创建回复



  let inputEl = document.querySelector('#input')
  let btnEl = document.querySelector('.btn1')
  // console.log(btnEl);

  btnEl.onclick = async function () {
    // 获取日记
    let res5 = await axios({
      method: "GET",
      url: "/api/diaryItem",
      params: {
        diaryItemId: '632325927542a3c04fc72c72',
      },

    })

    let riJI = res5.data.data.diaryItem.createAt;
    //请求作者名字
    let res3 = await axios({
      method: 'GET',
      url: '/api/user',
      params: {
        userId: user.userId
      }
    })
    console.log(666, res3);
    let userN = res3.data.data
    console.log(6633336, userN);
    // console.log(6667, riJI);
    let content = inputEl.value.trim();
    // console.log(44, content);
    //  发请求.添加数据
    let res1 = await axios({
      method: 'POST',
      url: '/api/reply/create',
      data: {
        content,
        authorId: '632197bd8c0fd333f646d17c',
        diaryItemId: '6322a2fd9d798f6c5224622d',
        createAt: 1683208023887
      }
    })
    let huiF = res1.data.data
    let get = res1.config.data
    let grtUp = JSON.parse(get)
    console.log(8889, grtUp);
    console.log(777, res1);
    console.log(66, huiF);
    let res2 = await axios({
      method: 'get',
      url: '/api/reply',
      params: {
        replyId: ' huiF'
      }
    })

    console.log(432, res2);
    commentsEl.innerHTML = `
      <div class="ry">
            <img src="http://localhost:8000${ userN.headphoto}" alt="">
            <div class="body">
              <span class="name">${ userN.username}</span>
              <span class="time">${formatDate(grtUp.createAt)}pm</span>
              <div class="tit">${grtUp.content}</div>
            
              </div>
          </div>
  
  `+ html1


    function formatDate(date) {
      date = new Date(date)

      let year = date.getFullYear()
      let month = (date.getMonth() + 1).toString().padStart(2, 0)
      let day = date.getDate().toString().padStart(2, 0)

      let hours = date.getHours().toString().padStart(2, 0);
      let minutes = date.getMinutes().toString().padStart(2, 0);
      let seconds = date.getSeconds().toString().padStart(2, 0);
      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
    }

  }
}










let worldEl = document.querySelector('.world');
test()
async function test() {

  let res = await axios({
    method: "GET",
    url: "/api/user",
    params: {
      userId: user.userId,
    },
  });


  let method = res.data.data
  console.log(918, method);
  html = `
       <div class="ys">
            <h2>${method.username}</h2>
            <img src="http://localhost:8000${method.headphoto}" alt="" >
            <div class="timeIn">${method.createAt}加入</div>
            <div class="Focus">关注此人</div>
     </div>
  `

  worldEl.innerHTML = html
}







